package com.hgm.flopgame2.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.hgm.flopgame2.R

class CardAdapter(var cardList: ArrayList<Int>) : RecyclerView.Adapter<CardAdapter.MyViewHolder>() {

    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val rootView: View = view.findViewById(R.id.rootView)
        val card1: ImageView = view.findViewById(R.id.item_card1)
        val card2: LinearLayout = view.findViewById(R.id.item_card2)
        val image: ImageView = view.findViewById(R.id.image)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        var view = LayoutInflater.from(parent.context).inflate(R.layout.item_home, null)
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        var type = cardList[position]
        //根据类型设置图片
        setImage(type, holder)
        //卡片点击事件
        holder.itemView.setOnClickListener {
            onItemClickListener?.onItemClick(holder,type,position,holder.rootView,holder.card1,holder.card2)
        }
    }

    private fun setImage(type: Int, holder: MyViewHolder) {
        when (type) {
            1 -> holder.image.setImageResource(R.drawable.buzhid)
            2 -> holder.image.setImageResource(R.drawable.chair)
            3 -> holder.image.setImageResource(R.drawable.dianfanbao)
            4 -> holder.image.setImageResource(R.drawable.errji)
            5 -> holder.image.setImageResource(R.drawable.fs)
            6 -> holder.image.setImageResource(R.drawable.shouyinji)
            7 -> holder.image.setImageResource(R.drawable.sofa_)
            8 -> holder.image.setImageResource(R.drawable.yundou)
        }
    }


    override fun getItemCount(): Int {
        return cardList.size
    }


    //定义item点击接口
    private var onItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
        this.onItemClickListener = onItemClickListener
    }

    interface OnItemClickListener {
        //holder、类型、下标、根视图、背面、卡面
        fun onItemClick(holder: MyViewHolder,type: Int,position: Int,rootView: View, view1: View, view2: View)
    }


}